package com.aceitunaproject.gameclient;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import com.aceitunaproject.client.EnumGameElementType;
import com.aceitunaproject.shared.gamelement.Installation;
import com.aceitunaproject.shared.gamelement.Settlement;
import com.aceitunaproject.shared.gamelement.Unit;

public class OrdersPanel extends JPanel implements ActionListener {

	private static final long serialVersionUID = 1L;
	private GameClient controller;
	private Unit theUnit;
	private EnumGameElementType type;
	// private Installation theInstallation;
	private Settlement theSettlement;

	public OrdersPanel(GameClient controller) {
		this.controller = controller;
		this.setBorder(BorderFactory.createEtchedBorder());
	}

	public void setForUnit(Unit unit) {

		theUnit = unit;
		// theInstallation = null;
		theSettlement = null;

		type = EnumGameElementType.Unit;

		this.removeAll();
		addButton("Refresh Map", "RefreshMap");
		addButton("Build Settlement", "BuildSettlement");
		addButton("Build Installation", "BuildInstallation");
		addButton("Attack Unit", "AttackUnit");

		this.revalidate();
		this.repaint();
	}

	public void setForInstallation(Installation installation) {

		theUnit = null;
		// theInstallation = installation;
		theSettlement = null;

		type = EnumGameElementType.Installation;

		this.removeAll();
		addButton("Refresh Map", "RefreshMap");
		this.revalidate();
		this.repaint();
	}

	public void setForSettlement(Settlement settlement) {

		theUnit = null;
		// theInstallation = null;
		theSettlement = settlement;

		type = EnumGameElementType.Settlement;

		this.removeAll();
		addButton("Refresh Map", "RefreshMap");
		addButton("Build Unit", "BuildUnit");

		this.revalidate();
		this.repaint();
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		if (e.getActionCommand().equals("RefreshMap")) {

			controller.orderedToRefreshMap();
		}
		
		if (e.getActionCommand().equals("Cancel")) {

			controller.orderedToCancel();
			
		}

		else

		{
			if(type == null)
				return;
			
			switch (type) {
			case Unit:

				if (e.getActionCommand().equals("BuildSettlement")) {
					controller.orderedToBuildSettlement(theUnit);
				}
				if (e.getActionCommand().equals("BuildInstallation")) {
					controller.orderedToBuildInstallation(theUnit);
				}
				if (e.getActionCommand().equals("AttackUnit")) {
					controller.orderedToAttackWithUnit();
				}
				break;

			case Settlement:

				if (e.getActionCommand().equals("BuildUnit")) {
					controller.orderedToBuildUnit(theSettlement);
				}

				break;

			default:
				break;
			}
		}
	}

	private void addButton(String text, String action) {

		JButton button = new JButton(text);
		button.setActionCommand(action);
		button.addActionListener(this);
		this.add(button);
	}

	public void setForNothing() {
		theUnit = null;
		// theInstallation = installation;
		theSettlement = null;

		type = null;

		this.removeAll();
		addButton("Refresh Map", "RefreshMap");
		this.revalidate();
		this.repaint();

	}

	public void setForCancelOnly() {
		this.removeAll();
		addButton("Cancel", "Cancel");
		this.revalidate();
		this.repaint();
	}

}
